package com.starkuz.controllers;

import com.starkuz.dao.ifaces.OrderDao;
import com.starkuz.dao.ifaces.UserDao;
import com.starkuz.dao.ifaces.leads.CtrDao;
import com.starkuz.dao.ifaces.leads.LeadDao;
import com.starkuz.dao.ifaces.sites.CtrSiteDao;
import com.starkuz.dao.ifaces.sites.LeadSiteDao;
import com.starkuz.dao.ifaces.sites.OrderSiteDao;
import com.starkuz.dao.ifaces.visits.VisitCtrDao;
import com.starkuz.dao.ifaces.visits.VisitLeadDao;
import com.starkuz.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@Controller
public class AdminToolsController {

    @Autowired
    private LeadSiteDao leadSiteDao;

    @Autowired
    private VisitLeadDao visitLeadDao;

    @Autowired
    private LeadDao leadDao;

    @Autowired
    private OrderSiteDao orderSiteDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private CtrSiteDao ctrSiteDao;

    @Autowired
    private CtrDao ctrDao;

    @Autowired
    private VisitCtrDao visitCtrDao;

    @Autowired
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void setCtrSiteDao(CtrSiteDao ctrSiteDao) {
        this.ctrSiteDao = ctrSiteDao;
    }

    public void setVisitLeadDao(VisitLeadDao visitLeadDao) {
        this.visitLeadDao = visitLeadDao;
    }

    public void setVisitCtrDao(VisitCtrDao visitCtrDao) {
        this.visitCtrDao = visitCtrDao;
    }

    public void setLeadSiteDao(LeadSiteDao leadSiteDao) {
        this.leadSiteDao = leadSiteDao;
    }

    public void setLeadDao(LeadDao leadDao) {
        this.leadDao = leadDao;
    }

    public void setOrderSiteDao(OrderSiteDao orderSiteDao) {
        this.orderSiteDao = orderSiteDao;
    }

    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }

    public void setCtrDao(CtrDao ctrDao) {
        this.ctrDao = ctrDao;
    }

    //админка
    @RequestMapping(value = "/admin.html", method = RequestMethod.GET)
    public String testPage(HttpServletRequest req, Map model) {
        if (!isAdminUser(req)){
            return "redirect:index.html";
        }
        List<User> users =  userDao.getAllUsers();
        Integer countUsers = users.size();
        model.put("countUsers", countUsers);
        return "admins";
    }

    private boolean isAdminUser(HttpServletRequest request){
        boolean answer = false;
        HttpSession session =  request.getSession();
        if (session.getAttribute("user") != null) {
            User user = (User)session.getAttribute("user");
            if (user.getStatus().equals(User.ADMIN)){
                answer = true;
            }
        }
        return answer;
    }
}
